// This file is a part of ChaosDB.
// Copyright (C) 2012 ChaoJianPeng
// email:chaojianpeng@gmail.com
//
// ChaosDB is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// ChaosDB is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

/*
 * byte_array.cpp
 *
 *  Created on: 2012-4-10
 *      Author: chao
 */

#include "byte_array.h"
#include "cmalloc.h"
#include "conf.h"
#include "logger.h"
#include <stdlib.h>
#include <string.h>

void creatReadByteArray(ByteArray* barr, void* data, int size) {
	barr->data = data;
	barr->size = size;
	barr->pos = 0;
}

int readInt(int* dest, ByteArray* barr) {
	if (barr->pos + SIZE_INT > barr->size) {
		return BYTE_ARRAY_EOF;
	}
	*dest = *((int*) (barr->data + barr->pos));
	barr->pos += SIZE_INT;
	return BYTE_ARRAY_OK;
}

int readString(char* dest, ByteArray* barr, int size) {
	if (barr->pos + size > barr->size) {
		return BYTE_ARRAY_EOF;
	}
	memcpy(dest, barr->data + barr->pos, size);
	barr->pos += size;
	dest[size] = '\0';
	return BYTE_ARRAY_OK;
}

int readBuff(void* dest, ByteArray* barr, int size) {
	if (barr->pos + size > barr->size) {
		return BYTE_ARRAY_EOF;
	}
	memcpy(dest, barr->data + barr->pos, size);
	barr->pos += size;
	return BYTE_ARRAY_OK;
}

/**
 *
 */
void creatWriteByteArray(ByteArray* barr) {
	barr->data = malloc(WRITE_BUFF_INIT_SIZE);
	barr->size = WRITE_BUFF_INIT_SIZE;
	barr->pos = 0;
}

bool reSize(ByteArray* barr, int minsize) {

	int newSize = barr->size;
	void* buff;
	if (minsize > MAX_RAM_USED) {
		systemLogger("ram full error!!!");
		return false;
	}
	while (newSize < minsize) {
		newSize <<= 2;
		if (newSize > MAX_RAM_USED) {
			newSize = minsize;
		}
	}

	buff = malloc(newSize);
	memcpy(buff, barr->data, barr->pos);
	free(barr->data);
	barr->data = buff;
	barr->size = newSize;

	return true;
}

int writeInt(const int* src, ByteArray* barr) {
	if (barr->pos + SIZE_INT > barr->size) {
		if (!reSize(barr, barr->pos + SIZE_INT))
			return BYTE_ARRAY_EOF;
	}
	*((int*) (barr->data + barr->pos)) = *src;
	barr->pos += SIZE_INT;
	return BYTE_ARRAY_OK;
}

int writeString(char* src, ByteArray* barr, int size) {
	if (barr->pos + size > barr->size) {
		if (!reSize(barr, barr->pos + size))
			return BYTE_ARRAY_EOF;
	}
	memcpy(barr->data + barr->pos, src, size);
	barr->pos += size;
	return BYTE_ARRAY_OK;
}

int writeBuff(void* src, ByteArray* barr, int size) {
	if (barr->pos + size > barr->size) {
		if (!reSize(barr, barr->pos + size))
			return BYTE_ARRAY_EOF;
	}
	memcpy(barr->data + barr->pos, src, size);
	barr->pos += size;
	return BYTE_ARRAY_OK;
}

void closeWriteByteArray(ByteArray* barr) {
	free(barr->data);
}
